package com.xdf.whiteaccount.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import com.xdf.whiteaccount.entity.DyeGreyDetail;
import com.xdf.whiteaccount.service.DyeGreyDetailService;
import com.xdf.whiteaccount.utils.JsonResult;

/**
 * 注意要有依赖：
 *         <dependency>
 *             <groupId>io.springfox</groupId>
 *             <artifactId>springfox-swagger2</artifactId>
 *             <version>2.9.2</version>
 *         </dependency>
 *         <dependency>
 *             <groupId>io.springfox</groupId>
 *             <artifactId>springfox-swagger-ui</artifactId>
 *             <version>2.9.2</version>
 *         </dependency>
 * 同时入口类头上使用注解：@EnableSwagger2
 * @Description : 控制层，代码生成器自动生成
 * @Author : chanchaw
 * @Date : 2023-12-15 16:08:04
 */
@Api(tags = "染厂白坯单据明细")
@CrossOrigin
@RestController
@RequestMapping("/dyeGreyDetail")
public class DyeGreyDetailController {
    @Autowired
    private DyeGreyDetailService service;

    // 全部属性新增
    @ApiOperation(value = "完全新增", httpMethod = "POST", notes = "推荐使用save请求")
    @PostMapping("/insert")
    public JsonResult insert(@RequestBody DyeGreyDetail record){
        return JsonResult.ok(service.insert(record));
    }

    // 选择属性新增。value=null 的属性不会写入DB，DB会填充默认值
    @ApiOperation(value = "选择性新增", httpMethod = "POST", notes = "推荐使用save请求")
    @PostMapping("/insertSelective")
    public JsonResult insertSelective(@RequestBody DyeGreyDetail record){
        return JsonResult.ok(service.insertSelective(record));
    }

    // 根据主键删除，切记表要有主键，否则为删除所有数据！！！！！！！！！！
    @ApiOperation(value = "根据主键删除", httpMethod = "POST", notes = "所有请求都是post")
    @PostMapping("/delete/{pk}")
    public JsonResult deleteByPrimaryKey(@PathVariable Integer pk){
        return JsonResult.ok(service.deleteByPrimaryKey(pk));
    }

    // 根据主键查询，切记表要有主键，否则为查询所有数据！！！！！！！！！！
    @ApiOperation(value = "根据主键查询", httpMethod = "POST", notes = "所有请求都是post")
    @PostMapping("/select/{pk}")
    public JsonResult selectByPrimaryKey(@PathVariable Integer pk){
        return JsonResult.ok(service.selectByPrimaryKey(pk));
    }

    // 获取所有数据
    @ApiOperation(value = "查询所有", httpMethod = "POST", notes = "所有请求都是post")
    @PostMapping("/selectAll")
    public JsonResult selectAll(){
        return JsonResult.ok(service.selectAll());
    }

    // 全部属性更新。前端传递来属性value=null则会设置后台数据为null
    @ApiOperation(value = "完全修改", httpMethod = "POST", notes = "推荐使用save方法")
    @PostMapping("/updateByPrimaryKey")
    public JsonResult updateByPrimaryKey(@RequestBody DyeGreyDetail record) {
        return JsonResult.ok(service.updateByPrimaryKey(record));
    }

    // 部分属性更新。前端传递来value=null的属性不会被更新到数据库
    @ApiOperation(value = "选择性修改", httpMethod = "POST", notes = "推荐使用save方法")
    @PostMapping("/updateByPrimaryKeySelective")
    public JsonResult updateByPrimaryKeySelective(@RequestBody DyeGreyDetail record) {
        return JsonResult.ok(service.updateByPrimaryKeySelective(record));
    }

    // 有则修改，无则新增。选择性保存，如果属性value=null则不会更新该字段
    @ApiOperation(value = "保存数据", httpMethod = "POST", notes = "主键为空则新增(选择性新增)，主键有值则修改（选择性修改）")
    @PostMapping("/save")
    public JsonResult save(@RequestBody DyeGreyDetail record) {
        return JsonResult.ok(service.save(record));
    }

    // 是上面接口 save 的批量执行版本
    @ApiOperation(value = "对象数组保存", httpMethod = "POST", notes = "所有请求都是post")
    @PostMapping("/multiSave")
    public JsonResult multiSave(@RequestBody List<DyeGreyDetail> records) {
        return JsonResult.ok(service.multiSave(records));
    }
    //=================以上是自动生成的代码，自定义代码请写在下面==================
    @ApiOperation(value = "选择性修改", httpMethod = "POST", notes = "推荐使用save方法")
    @PostMapping("/updateBatch")
    public JsonResult updateBatch(@RequestBody List<DyeGreyDetail> detailList) {
        if(detailList == null || detailList.size() == 0) return JsonResult.ok("没有传入数据，没有执行修改！");
        for (DyeGreyDetail item : detailList) {
            service.updateByPrimaryKeySelective(item);
        }
        return JsonResult.ok("修改行数：" + detailList.size());
    }
}